import React, { Component } from 'react'
import "./index.css"
export default class index extends Component {

    state = {
        count: 0,
        step: 1
    }

    // componentDidMount() {
    //     console.log(this.refs);
    // }

    selectChange() {
        const { value } = this.selectNumber
        this.setState({
            step: parseInt(value),
        })
    }

    increment = () => {
        const { count, step } = this.state
        this.setState({
            count: count + step
        })
    }

    decrement = () => {
        const { count, step } = this.state

        this.setState({
            count: count - step
        })
    }

    incrementIfOdd = () => {
        const { count, step } = this.state


        if (count % 2 !== 0) {
            this.setState({
                count: count + step
            })
        }
    }

    incrementIfEvent = () => {
        const { count, step } = this.state


        if (count % 2 == 0) {
            this.setState({
                count: count + step
            })
        }
    }

    // 异步加
    incrementAsync = () => {
        const { count, step } = this.state

        setTimeout(() => {
            this.setState({
                count: count + step
            })
        }, 500)
    }
    render() {
        return (
            <div>
                <h1>redux的使用</h1>
                <h1>当前求和为:{this.state.count}</h1>
                <select onChange={this.selectChange.bind(this)} ref={c => { this.selectNumber = c }}>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                </select>
                <button onClick={this.increment}>+</button>
                <button onClick={this.decrement}>-</button>
                <button onClick={this.incrementIfOdd}>当前求和为奇数在加</button>
                <button onClick={this.incrementIfEvent}>当前求和为偶数在加</button>
                <button onClick={this.incrementAsync}>当前求和为偶数在加</button>
            </div>
        )
    }
}
